package com.handsome.ndkvoice;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import org.fmod.FMOD;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private final String TAG = "NDK_Voice";
    private ExecutorService fixedThreadPool;
    private PlayerThread playerThread;
    private String path = "file:///android_asset/cool.wav";
    private String bgmpath = "file:///android_asset/music_lvl_03_doma_east_theme_bpm_140.ogg";
    private int type;

    private LinearLayout normal, luoli, dashu, jingsong, gaoguai, kongling;
    private RelativeLayout playState, bgmplayState;
    private SeekBar sb_1, sb_2, sb_3, sb_4, sb_5, sb_6, sb_7, sb_8, sb_9, sb_10, sb_vol, sb_bgmvol;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        normal = (LinearLayout) findViewById(R.id.normal);
        luoli = (LinearLayout) findViewById(R.id.luoli);
        dashu = (LinearLayout) findViewById(R.id.dashu);
        jingsong = (LinearLayout) findViewById(R.id.jingsong);
        gaoguai = (LinearLayout) findViewById(R.id.gaoguai);
        kongling = (LinearLayout) findViewById(R.id.kongling);
        playState = (RelativeLayout) findViewById(R.id.rl_play);
        bgmplayState = (RelativeLayout) findViewById(R.id.rl_bgmplay);
        normal.setOnClickListener(this);
        luoli.setOnClickListener(this);
        dashu.setOnClickListener(this);
        jingsong.setOnClickListener(this);
        gaoguai.setOnClickListener(this);
        kongling.setOnClickListener(this);
        playState.setOnClickListener(this);
        bgmplayState.setOnClickListener(this);

        sb_1 = (SeekBar) findViewById(R.id.seekBar1);
        sb_2 = (SeekBar) findViewById(R.id.seekBar2);
        sb_3 = (SeekBar) findViewById(R.id.seekBar3);
        sb_4 = (SeekBar) findViewById(R.id.seekBar4);
        sb_5 = (SeekBar) findViewById(R.id.seekBar5);
        sb_6 = (SeekBar) findViewById(R.id.seekBar6);
        sb_7 = (SeekBar) findViewById(R.id.seekBar7);
        sb_8 = (SeekBar) findViewById(R.id.seekBar8);
        sb_9 = (SeekBar) findViewById(R.id.seekBar9);
        sb_10 = (SeekBar) findViewById(R.id.seekBar10);
        sb_vol = (SeekBar) findViewById(R.id.SeekBarVol);
        sb_bgmvol = (SeekBar) findViewById(R.id.SeekBarBGMVol);

        sb_1.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_2.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_3.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_4.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_5.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_6.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_7.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_8.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_9.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_10.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_vol.setOnSeekBarChangeListener(new OnSeekBarChangeListener());
        sb_bgmvol.setOnSeekBarChangeListener(new OnSeekBarChangeListener());

        fixedThreadPool = Executors.newFixedThreadPool(1);
        FMOD.init(this);
    }
    private int bar_match(SeekBar sbar)
    {
        int ret = 0;
        switch (sbar.getId())
        {
            case R.id.seekBar1:
                ret = 0;
                break;
            case R.id.seekBar2:
                ret = 1;
                break;
            case R.id.seekBar3:
                ret = 2;
                break;
            case R.id.seekBar4:
                ret = 3;
                break;
            case R.id.seekBar5:
                ret = 4;
                break;
            case R.id.seekBar6:
                ret = 5;
                break;
            case R.id.seekBar7:
                ret = 6;
                break;
            case R.id.seekBar8:
                ret = 7;
                break;
            case R.id.seekBar9:
                ret = 8;
                break;
            case R.id.seekBar10:
                ret = 9;
                break;
            case R.id.SeekBarVol:
                ret = 0xEE;
                break;
            case R.id.SeekBarBGMVol:
                ret = 0xEF;
                break;
            default:
                ret = 0;
                break;
        }
        return ret;
    }

    private class OnSeekBarChangeListener implements
            SeekBar.OnSeekBarChangeListener {
        private int curprogress[] = new int[10];
        private int chvolgress = 0;
        private int bar_index = 0;

        // 触发操作，拖动
        public void onProgressChanged(SeekBar seekBar, int progress,
                                      boolean fromUser) {
            if((bar_match(seekBar) != 0xEE) && (bar_match(seekBar) != 0xEF))
            {
                curprogress[bar_match(seekBar)] = progress;
                Log.i(TAG, "onProgressChanged: "+ bar_match(seekBar) +" progress:"+ progress);
            }
            else
            {
                //音量
                chvolgress = progress;
            }
        }

        // 表示进度条刚开始拖动，开始拖动时候触发的操作
        public void onStartTrackingTouch(SeekBar seekBar) {

            Log.i(TAG, "onStartTrackingTouch:");
        }

        // 停止拖动时候
        public void onStopTrackingTouch(SeekBar seekBar) {
            if((bar_match(seekBar) != 0xEE) && (bar_match(seekBar) != 0xEF)) {
                Log.i(TAG, "onStopTrackingTouch" + curprogress[bar_match(seekBar)]);
                Utils.SetEQ(bar_match(seekBar), curprogress[bar_match(seekBar)], 0);
            }
            else
            {
                Log.i(TAG, "onStopTrackingTouch" +bar_match(seekBar)+ "level" + chvolgress);
                if(bar_match(seekBar) == 0xEF)
                    Utils.SetChannelVolume(1, chvolgress);
                else
                    Utils.SetChannelVolume(0, chvolgress);
            }
        }
    }

    class PlayerThread implements Runnable {
        @Override
        public void run() {
            Utils.main(path, bgmpath, type);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.normal:
                Log.i(TAG, "normal");
                Utils.SetReveration(Utils.MODE_NORMAL);
                break;
            case R.id.luoli:
                Log.i(TAG, "luoli1 1");
                Utils.SetReveration(Utils.MODE_LUOLI);
                break;
            case R.id.dashu:
//                type = Utils.MODE_DASHU;
                Utils.SetReveration(Utils.MODE_DASHU);
                break;
            case R.id.jingsong:
//                type = Utils.MODE_JINGSONG;
                Utils.SetReveration(Utils.MODE_JINGSONG);
                break;
            case R.id.gaoguai:
//                type = Utils.MODE_GAOGUAI;

                Utils.SetReveration(Utils.MODE_GAOGUAI);
                break;
            case R.id.kongling:
//                type = Utils.MODE_KONGLING;

                Utils.SetReveration(Utils.MODE_KONGLING);
                break;
            case R.id.rl_play:
                Log.i(TAG, "rl_play");
                Utils.PlayState(1);
                break;
            case R.id.rl_bgmplay:
                Utils.PlayState(0x10);
                break;

        }

        playerThread = new PlayerThread();
        fixedThreadPool.execute(playerThread);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FMOD.close();
    }
}
